#include "lcd_2_8.h"
#include "spi_dev.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "pwm_dev.h"
#include "f_lcd_gui.h"

#define LCD_PIN_VCC 44

static void write_cmd(uint8_t cmd)
{
    spi_wr_cmd(cmd);
}
static void write_data(uint8_t data)
{
    spi_wr_byte(data);
}

void lcd_task(void)
{
    pwm_init();
    lcd_hw_init();
    lcd_clear();
    vTaskDelay(pdMS_TO_TICKS(50));
}


void lcd_hw_init(void)
{
    gpio_output_init(PIN_BK_LIGHT,1,0);
    hal_gpio_outSet(PIN_BK_LIGHT,0);
}

void lcd_rst(void)
{
    hal_gpio_outSet(LCD_PIN_RST,0);
    vTaskDelay(pdMS_TO_TICKS(3));
    hal_gpio_outSet(LCD_PIN_RST,1);
    vTaskDelay(pdMS_TO_TICKS(120));
}

void lcd_2_8_init(void)
{
	write_cmd(0x11);
    vTaskDelay(pdMS_TO_TICKS(10));
 
	write_cmd(0x36);
	write_data (0xa0);
    
	write_cmd(0x3a);
	write_data (0x05);      //像素点的数据格式设置为 RGB：565
 
    //与屏的型号有关
	write_cmd(0x20);  // 颜色值需按位取反
 
	write_cmd(0xb2);
	write_data (0x0c); 
	write_data (0x0c); 
	write_data (0x00); 
	write_data (0x33);
	write_data (0x33);
	 
	write_cmd(0xb7);
	write_data (0x35);
 
	write_cmd(0xbb);
	write_data (0x35);
	write_cmd(0xc0);
	write_data (0x2c);
	write_cmd(0xc2);
	write_data (0x01);
	write_cmd(0xc3);
	write_data (0x0b);
	write_cmd(0xc4);
	write_data (0x20);
	write_cmd(0xc6);
	write_data (0x1e);
	write_cmd(0xd0);
	write_data (0xa4);
	write_data (0xa1);
 
	write_cmd(0xE0);    //Set Gamma
	write_data (0xd0);
	write_data (0x00);
	write_data (0x02);
	write_data (0x07);
	write_data (0x0b);
	write_data (0x1a);
	write_data (0x31);
	write_data (0x54);
	write_data (0x40);
	write_data (0x29);
	write_data (0x12);
	write_data (0x12);
	write_data (0x12);
	write_data (0x17);
	
	write_cmd(0XE1);    //Set Gamma 
	write_data (0xd0);
	write_data (0x00);
	write_data (0x02);
	write_data (0x07);
	write_data (0x05);
	write_data (0x25);
	write_data (0x2d);
	write_data (0x44);
	write_data (0x45);
	write_data (0x1c);
	write_data (0x18);
	write_data (0x16);
	write_data (0x1c);
	write_data (0x1d);
	write_cmd(0x29);
}
